package com.wwwarehouse.contract.program_operation;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.adapter.PinCardAdapter;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.bean.CustomerAndResBean;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.bean.SaveModulesBean;
import com.wwwarehouse.contract.bean.SaveModulesValue;
import com.wwwarehouse.contract.bean.SourceListBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.CheckAllResFactorEvent;
import com.wwwarehouse.contract.event.CheckCustomersEvent;
import com.wwwarehouse.contract.event.CheckIndusEvent;
import com.wwwarehouse.contract.event.CheckResEvent;
import com.wwwarehouse.contract.event.CheckResourceFactorShipEvent;
import com.wwwarehouse.contract.event.CheckResourceShipEvent;
import com.wwwarehouse.contract.event.CheckSupplyEvent;
import com.wwwarehouse.contract.program_operation.resource_factor_ship.ResourceFactorShipViewPagerFragment;
import com.wwwarehouse.contract.program_operation.resource_ship.ResourceShipViewPagerFragment;
import com.wwwarehouse.contract.program_operation.resources_factor_page.ResourceFactorMainFragment;
import com.wwwarehouse.contract.program_operation.resources_page.SupplyOfResourceViewPagerFragment;
import com.wwwarehouse.contract.program_operation.select_industry.SelectIndustryViewPagerFragment;
import com.wwwarehouse.contract.request.ModuleQueryRequest;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/21.
 * 任务卡入口进来的 拼卡主页
 */
@Route(path = "/contract/TaskPinCardMainFragment")
public class TaskPinCardMainFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
	private LinearLayout mIndusLayout; //顶部行业布局
	private LinearLayout mResourceShipLayout; //底部资源关系
	private LinearLayout mResourceFactorShipLayout; //底部资源要素关系按钮
	private MyListView mClientLv, mResourceLv, mResourceFactorLv, mSupplierLv;   //客户 、 资源、资源要素、供应商 4个竖直拼卡
	private TextView mIndustryTv; //行业文本
	private TextView mCardTv;
	private PinCardAdapter mClientAdapter, mResourceAdapter, mResourceFactorAdapter, mSupplierAdapter;
	private ImageView mResourceShipIv, mResourceFactorShipIv;
	private TextView mResourceShipTv, mResourceFactorShipTv;
	//4个listView的实体数据
	private List<ChooseModuleBean.ModuleList.BeanList> mClientPinCardBeanList = new ArrayList<>();
	private List<ChooseModuleBean.ModuleList.BeanList> mResourcePinCardBeanList = new ArrayList<>();
	private List<ChooseModuleBean.ModuleList.BeanList> mResourceFactorPinCardBeanList = new ArrayList<>();
	private List<ChooseModuleBean.ModuleList.BeanList> mSupplierPinCardBeanList = new ArrayList<>();

	private List<ChooseModuleBean.ModuleList.BeanList> mIndustryCheckedList = new ArrayList<>(); //行业已选的条目
	private List<ChooseModuleBean.ModuleList.BeanList> mCustomerCheckedList = new ArrayList<>(); //客户已选的条目
	private List<ChooseModuleBean.ModuleList.BeanList> mResourceCheckedList = new ArrayList<>(); //资源已选的条目
	private List<ChooseModuleBean.ModuleList.BeanList> mNeedResourceFactorCheckedList = new ArrayList<>(); //需要的资源要素已选的条目
	private List<ChooseModuleBean.ModuleList.BeanList> mHaveResourceFactorCheckedList = new ArrayList<>(); //拥有的资源要素已选的条目

	private List<ChooseModuleBean.ModuleList.BeanList> mSupplierCheckedList = new ArrayList<>(); //供应商已选的条目
	int mLvHeight;
	CustomerAndResBean mCustomerAndResBean;

	CustomerAndResBean mRecourseFactorShipBean;
	CustomerAndResBean mYuanShuJu;

	ElasticScrollView mElasticScrollView;
	Button titleBt1;
	String mBusId;
	String mCardUkid;
	boolean isShow;

	@Override
	public void setTitle() {
		if (peekFragment() instanceof TaskPinCardMainFragment) {
			mActivity.setTitle(R.string.programming_operation_title);
		}
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.pincard_main_layout, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (!sp.getBooleanValue(ContractConstant.SP_KEY_PROGRAM_POERATION_GUIDE)) {
			Intent intent = new Intent();
			intent.setClass(mActivity, ContractGuidePageActivity.class);
			startActivity(intent);
		}

		if (getArguments() != null && getArguments().getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS) != null) {
			CardDeskTaskResponseBean.TaskBean taskBean = (CardDeskTaskResponseBean.TaskBean) getArguments().getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS);
			if (taskBean != null && taskBean.getBelongBusiness() != null && taskBean.getCardUkid() != null) {
				mBusId = taskBean.getBelongBusiness();
				mCardUkid = taskBean.getCardUkid();
			}
		}
		initView(view);
		initEvent();
	}

	/**
	 * 返回参数List
	 *
	 * @return
	 */
	private List<SourceListBean> getSourceList(String type) {
		List<SourceListBean> mAllList = new ArrayList<>();
		if (mIndustryCheckedList != null && mIndustryCheckedList.size() > 0 && !type.equals("INDUSTRY")) {
			traverseList(mAllList, mIndustryCheckedList, "INDUSTRY");
		}
		if (mCustomerCheckedList != null && mCustomerCheckedList.size() > 0 && !type.equals("CUSTOMER")) {
			traverseList(mAllList, mCustomerCheckedList, "CUSTOMER");
		}
		if (mResourceCheckedList != null && mResourceCheckedList.size() > 0 && !type.equals("RESOURCE")) {
			traverseList(mAllList, mResourceCheckedList, "RESOURCE");
		}
		if (mHaveResourceFactorCheckedList != null && mHaveResourceFactorCheckedList.size() > 0 && !type.equals("HAVE_RESOURCE_FACTORS")) {
			traverseList(mAllList, mHaveResourceFactorCheckedList, "HAVE_RESOURCE_FACTORS");
		}
		if (mNeedResourceFactorCheckedList != null && mNeedResourceFactorCheckedList.size() > 0 && !type.equals("NEED_RESOURCE_FACTORS")) {
			traverseList(mAllList, mNeedResourceFactorCheckedList, "NEED_RESOURCE_FACTORS");
		}
		if (mSupplierCheckedList != null && mSupplierCheckedList.size() > 0 && !type.equals("PROVIDER")) {
			traverseList(mAllList, mSupplierCheckedList, "PROVIDER");
		}
		return mAllList;


	}

	//遍历List
	private List<SourceListBean> traverseList(List<SourceListBean> list, List<ChooseModuleBean.ModuleList.BeanList> beanLists, String type) {
		if (beanLists.size() > 0) {
			for (int i = 0; i < beanLists.size(); i++) {
				SourceListBean sourceListBean = new SourceListBean();
				sourceListBean.setCode(beanLists.get(i).getCode());
				sourceListBean.setModule(type);
				list.add(sourceListBean);
			}
		}
		return list;
	}

	@Override
	public void titleBt1Click() {
		if (!(peekFragment() instanceof TaskPinCardMainFragment)) {
			return;
		}
		if (mCustomerAndResBean == null && mRecourseFactorShipBean == null) {
			ToastUtils.showToast("没有选择提供服务关系或采购资源关系，无法完成！");
			if (isShow) {
				((BaseCardDeskActivity) mActivity).hideTitleBt();
			}
			isShow = !isShow;
			return;
		}

		showProgressDialog("正在请求数据...");
		Map<String, Object> paramsMap = new HashMap<>();
		if (mBusId != null) {
			paramsMap.put("buId", mBusId);
		}
		if (mCardUkid != null) {
			paramsMap.put("cardUkid", mCardUkid);
		}
		paramsMap.put("taskType", "task");

		if (mCustomerAndResBean != null) {
			CustomerAndResBean customerAndResBean = new CustomerAndResBean();
			customerAndResBean.setListBeen(mCustomerAndResBean.getListBeen());

			for (int i = customerAndResBean.getListBeen().size() - 1; i >= 0; i--) {
				for (int j = customerAndResBean.getListBeen().get(i).getList().size() - 1; j >= 0; j--) {
					if (!customerAndResBean.getListBeen().get(i).getList().get(j).isSelected()) {
						customerAndResBean.getListBeen().get(i).getList().remove(j);
					}

				}

			}

			for (int i = customerAndResBean.getListBeen().size() - 1; i >= 0; i--) {
				if (customerAndResBean.getListBeen().get(i).getList().size() == 0) {
					customerAndResBean.getListBeen().remove(i);
				}
			}
			Map<String, List<String>> map = new HashMap<>();
			for (int i = 0; i < customerAndResBean.getListBeen().size(); i++) {
				List<String> list = new ArrayList<String>();
				for (int j = 0; j < customerAndResBean.getListBeen().get(i).getList().size(); j++) {
					list.add(customerAndResBean.getListBeen().get(i).getList().get(j).getCode());
					map.put(customerAndResBean.getListBeen().get(i).getCode(), list);
				}
			}
			//拿到过滤之后的数据 ，里面只有关联过的数据
			List<SaveModulesValue> customerAndResourceParam = new ArrayList<SaveModulesValue>();
			for (Map.Entry<String, List<String>> entry : map.entrySet()) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(entry.getKey());
				saveModulesValue.setValue(listToString(entry.getValue()));
				customerAndResourceParam.add(saveModulesValue);
			}

			//资源关系参数
			paramsMap.put("customerAndResourceMap", customerAndResourceParam);

		}
		if (mCustomerCheckedList != null && mCustomerCheckedList.size() > 0) {
			List<SaveModulesValue> customerParams = new ArrayList<SaveModulesValue>();
			for (int i = 0; i < mCustomerCheckedList.size(); i++) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(mCustomerCheckedList.get(i).getCode());
				saveModulesValue.setValue(String.valueOf(mCustomerCheckedList.get(i).getProgress()));
				customerParams.add(saveModulesValue);
			}
			paramsMap.put("customerMap", customerParams);

		}

		if (mHaveResourceFactorCheckedList != null && mHaveResourceFactorCheckedList.size() > 0) {
			List<SaveModulesValue> haveResourceFactorParams = new ArrayList<SaveModulesValue>();
			for (int i = 0; i < mHaveResourceFactorCheckedList.size(); i++) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(mHaveResourceFactorCheckedList.get(i).getCode());
				saveModulesValue.setValue(String.valueOf(mHaveResourceFactorCheckedList.get(i).getProgress()));
				haveResourceFactorParams.add(saveModulesValue);
			}
			paramsMap.put("haveResourceFactorsMap", haveResourceFactorParams);

		}
		if (mIndustryCheckedList != null && mIndustryCheckedList.size() > 0) {
			List<SaveModulesValue> industryParams = new ArrayList<SaveModulesValue>();
			for (int i = 0; i < mIndustryCheckedList.size(); i++) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(mIndustryCheckedList.get(i).getCode());
				saveModulesValue.setValue(String.valueOf(mIndustryCheckedList.get(i).getProgress()));
				industryParams.add(saveModulesValue);
			}
			paramsMap.put("industryMap", industryParams);
		}

		if (mNeedResourceFactorCheckedList != null && mNeedResourceFactorCheckedList.size() > 0) {
			List<SaveModulesValue> needResourceFactorParams = new ArrayList<SaveModulesValue>();
			for (int i = 0; i < mNeedResourceFactorCheckedList.size(); i++) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(mNeedResourceFactorCheckedList.get(i).getCode());
				saveModulesValue.setValue(String.valueOf(mNeedResourceFactorCheckedList.get(i).getProgress()));
				needResourceFactorParams.add(saveModulesValue);
			}
			paramsMap.put("needResourceFactorsMap", needResourceFactorParams);
		}
/**
 * 资源关系 ===============================================================================================
 */
		if (mRecourseFactorShipBean != null) {
			CustomerAndResBean customerAndResBean = new CustomerAndResBean();
			customerAndResBean.setListBeen(mRecourseFactorShipBean.getListBeen());

			for (int i = customerAndResBean.getListBeen().size() - 1; i >= 0; i--) {
				for (int j = customerAndResBean.getListBeen().get(i).getList().size() - 1; j >= 0; j--) {
					if (!customerAndResBean.getListBeen().get(i).getList().get(j).isSelected()) {
						customerAndResBean.getListBeen().get(i).getList().remove(j);
					}

				}

			}

			for (int i = customerAndResBean.getListBeen().size() - 1; i >= 0; i--) {
				if (customerAndResBean.getListBeen().get(i).getList().size() == 0) {
					customerAndResBean.getListBeen().remove(i);
				}
			}
			Map<String, List<String>> map = new HashMap<>();
			for (int i = 0; i < customerAndResBean.getListBeen().size(); i++) {
				List<String> list = new ArrayList<String>();
				for (int j = 0; j < customerAndResBean.getListBeen().get(i).getList().size(); j++) {
					list.add(customerAndResBean.getListBeen().get(i).getList().get(j).getCode());
					map.put(customerAndResBean.getListBeen().get(i).getCode(), list);
				}
			}
			//拿到过滤之后的数据 ，里面只有关联过的数据
			List<SaveModulesValue> customerAndResourceParam = new ArrayList<SaveModulesValue>();
			for (Map.Entry<String, List<String>> entry : map.entrySet()) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(entry.getKey());
				saveModulesValue.setValue(listToString(entry.getValue()));
				customerAndResourceParam.add(saveModulesValue);
			}

			//资源关系参数
			paramsMap.put("providerAndResourceFactorsMap", customerAndResourceParam);

		}
		/**
		 * 资源关系结束 ===============================================================================================
		 */
		if (mSupplierCheckedList.size() > 0) {
			List<SaveModulesValue> supplierCheckParams = new ArrayList<SaveModulesValue>();
			for (int i = 0; i < mSupplierCheckedList.size(); i++) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(mSupplierCheckedList.get(i).getCode());
				saveModulesValue.setValue(String.valueOf(mSupplierCheckedList.get(i).getProgress()));
				supplierCheckParams.add(saveModulesValue);
			}
			paramsMap.put("providerMap", supplierCheckParams);
		}

		if (mResourceCheckedList.size() > 0) {
			List<SaveModulesValue> resourceCheckParams = new ArrayList<SaveModulesValue>();
			for (int i = 0; i < mResourceCheckedList.size(); i++) {
				SaveModulesValue saveModulesValue = new SaveModulesValue();
				saveModulesValue.setCode(mResourceCheckedList.get(i).getCode());
				saveModulesValue.setValue(String.valueOf(mResourceCheckedList.get(i).getProgress()));
				resourceCheckParams.add(saveModulesValue);
			}
			paramsMap.put("resourceMap", resourceCheckParams);
		}
		NoHttpUtils.httpPost(ContractConstant.PLANNING_SAVE_MODULES, paramsMap, new NoHttpUtils.OnResponseListener() {
			@Override
			public void onSucceed(CommonClass commonClass, int i) {
				dismissProgressDialog();
				if (commonClass != null && commonClass.getData() != null && commonClass.getCode().equals("0")) {
					SaveModulesBean.DataBean saveModulesBean = JSON.parseObject(commonClass.getData().toString(), SaveModulesBean.DataBean.class);
					mCardTv.setText(saveModulesBean.getCardCount() + "张卡片");

					DialogTools.getInstance().showCustomWarning(mActivity, "规划业务完成", "恭喜您获得" + saveModulesBean.getCardCount() + "张卡片", true, new DialogTools.ConfirmListener() {
						@Override
						public void setConfirmListener(Dialog dialog, View view, String s) {
							dialog.dismiss();
							EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
						}
					});
				} else {
					ToastUtils.showToast(commonClass.getMsg());
				}

			}

			@Override
			public void onFailed(String s, int i) {
				dismissProgressDialog();

			}

			@Override
			public void onFinish(int i) {
				dismissProgressDialog();
				if (isShow) {
					((BaseCardDeskActivity) mActivity).hideTitleBt();
				}
				isShow = !isShow;

			}
		}, 0);
	}

	//初始化控件
	private void initView(View view) {
		EventBus.getDefault().register(this);
		mIndusLayout = (LinearLayout) view.findViewById(R.id.indus_layout);
		mResourceFactorShipLayout = (LinearLayout) view.findViewById(R.id.resource_factor_ship_layout);
		mClientLv = (MyListView) view.findViewById(R.id.client_lv);
		mCardTv = (TextView) view.findViewById(R.id.card_tv);
		mIndustryTv = (TextView) view.findViewById(R.id.indus_tv);
		mResourceLv = (MyListView) view.findViewById(R.id.resource_lv);
		mResourceFactorLv = (MyListView) view.findViewById(R.id.resource_factor_lv);
		mSupplierLv = (MyListView) view.findViewById(R.id.supplier_lv);
		mResourceShipIv = (ImageView) view.findViewById(R.id.resource_ship_iv);
		mResourceShipTv = (TextView) view.findViewById(R.id.resource_ship_tv);
		mResourceFactorShipIv = (ImageView) view.findViewById(R.id.resource_factor_iv);
		mResourceFactorShipTv = (TextView) view.findViewById(R.id.resource_factor_tv);
		mResourceShipLayout = (LinearLayout) view.findViewById(R.id.resource_ship_layout);
		mElasticScrollView = (ElasticScrollView) view.findViewById(R.id.ecl_custom);
		mElasticScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
			@Override
			public void onDownPull() {
				if (!isShow) {
					((BaseCardDeskActivity) mActivity).downPull_3();
					((BaseCardDeskActivity) mActivity).showTitleBt1();
					((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);

				} else {
					((BaseCardDeskActivity) mActivity).hideTitleBt();
				}
				isShow = !isShow;

			}

			@Override
			public void onUpPull() {

			}
		});
		((BaseCardDeskActivity) mActivity).setTitleBt1Text("完成");
		mResourceShipLayout.setOnClickListener(this);
		mResourceFactorShipLayout.setOnClickListener(this);
		mResourceShipLayout.setEnabled(false);
		mResourceFactorShipLayout.setEnabled(false);
		mResourceShipLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
		mResourceFactorShipLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));

	}


	//点击事件
	private void initEvent() {
		mIndusLayout.setOnClickListener(this);
		mClientLv.setOnItemClickListener(this);
		mResourceLv.setOnItemClickListener(this);
		mResourceFactorLv.setOnItemClickListener(this);
		mSupplierLv.setOnItemClickListener(this);
	}

	@Override
	public void requestDatas() {
		ChooseModuleBean.ModuleList.BeanList beanList = new ChooseModuleBean.ModuleList.BeanList();
		beanList.setName("客户类型");
		beanList.setProgress(100);
		beanList.setShowPercent(false);
		mClientPinCardBeanList.add(beanList);


		ChooseModuleBean.ModuleList.BeanList beanList1 = new ChooseModuleBean.ModuleList.BeanList();
		beanList1.setName("提供的服务");
		beanList1.setProgress(1);
		mResourcePinCardBeanList.add(beanList1);

		ChooseModuleBean.ModuleList.BeanList beanList2 = new ChooseModuleBean.ModuleList.BeanList();
		beanList2.setName("需要采购和拥有的资源");
		beanList2.setProgress(1);
		mResourceFactorPinCardBeanList.add(beanList2);

		ChooseModuleBean.ModuleList.BeanList beanList3 = new ChooseModuleBean.ModuleList.BeanList();
		beanList3.setName("供应商类型");
		beanList.setShowPercent(false);
		beanList3.setProgress(100);
		mSupplierPinCardBeanList.add(beanList3);


		mClientLv.post(new Runnable() {
			@Override
			public void run() {
				mLvHeight = mClientLv.getHeight();
				mClientAdapter = new PinCardAdapter(mClientPinCardBeanList, mActivity, mLvHeight, R.layout.client_pincard_item);
				mClientLv.setAdapter(mClientAdapter);

				mResourceAdapter = new PinCardAdapter(mResourcePinCardBeanList, mActivity, mLvHeight, R.layout.rescource_pincard_item);
				mResourceLv.setAdapter(mResourceAdapter);

				mResourceFactorAdapter = new PinCardAdapter(mResourceFactorPinCardBeanList, mActivity, mLvHeight, R.layout.rescource_factort_pincard_item);
				mResourceFactorLv.setAdapter(mResourceFactorAdapter);

				mSupplierAdapter = new PinCardAdapter(mSupplierPinCardBeanList, mActivity, mLvHeight, R.layout.supplier_pincard_item);
				mSupplierLv.setAdapter(mSupplierAdapter);


			}
		});

	}

	@Override
	public void onClick(View v) {
		if (isShow) {
			((BaseCardDeskActivity) mActivity).hideTitleBt();
		}
		isShow = !isShow;
		int clickId = v.getId();
		//顶部行业点击事件
		if (clickId == R.id.indus_layout) {
			//上一次点击过存下来的带过去 ，没有就直接过去
			if (mIndustryCheckedList.size() > 0) {
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_CHECKED_DATA, (Serializable) mIndustryCheckedList);
				SelectIndustryViewPagerFragment selectIndustryViewPagerFragment = new SelectIndustryViewPagerFragment();
				selectIndustryViewPagerFragment.setArguments(bundle);
				pushFragment(selectIndustryViewPagerFragment);
			} else {
				pushFragment(new SelectIndustryViewPagerFragment());
			}
			//资源关系点击
		} else if (clickId == R.id.resource_ship_layout) {
			List<CustomerAndResListBean> list = new ArrayList<>();
			for (int i = 0; i < mResourceCheckedList.size(); i++) {
				CustomerAndResListBean customerAndResBean = new CustomerAndResListBean();
				customerAndResBean.setName(mResourceCheckedList.get(i).getName());
				customerAndResBean.setCode(mResourceCheckedList.get(i).getCode());
				customerAndResBean.setUrl(mResourceCheckedList.get(i).getImageUrl().getSelectedUrl());

				List<CustomerAndResListBean.Customers> customerses = new ArrayList<>();
				for (int j = 0; j < mCustomerCheckedList.size(); j++) {
					CustomerAndResListBean.Customers customers = new CustomerAndResListBean.Customers();
					customers.setCode(mCustomerCheckedList.get(j).getCode());
					customers.setName(mCustomerCheckedList.get(j).getName());
					customers.setSelected(mCustomerCheckedList.get(j).isSelect());
					customerses.add(customers);
				}
				customerAndResBean.setList(customerses);
				list.add(customerAndResBean);
			}
			//组合好的新数据list 所有要用到的数据塞到fragment中
			CustomerAndResBean customerAndResBean = new CustomerAndResBean();
			customerAndResBean.setListBeen(list);
			Bundle bundle = new Bundle();
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA, customerAndResBean);
			if (mCustomerAndResBean != null) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_LAST_RESCOURCE_SHIP_CHECKED, mCustomerAndResBean);
			}
			ResourceShipViewPagerFragment resourceShipViewPagerFragment = new ResourceShipViewPagerFragment();
			resourceShipViewPagerFragment.setArguments(bundle);
			pushFragment(resourceShipViewPagerFragment);
		} else if (clickId == R.id.resource_factor_ship_layout) {
/**
 *   资源要素关系点击事件
 */

			List<CustomerAndResListBean> list = new ArrayList<>();
			for (int i = 0; i < mSupplierCheckedList.size(); i++) {
				CustomerAndResListBean supplyAndResFactorBean = new CustomerAndResListBean();
				supplyAndResFactorBean.setName(mSupplierCheckedList.get(i).getName());
				supplyAndResFactorBean.setCode(mSupplierCheckedList.get(i).getCode());
				supplyAndResFactorBean.setUrl(mSupplierCheckedList.get(i).getImageUrl().getDefaultUrl());

				List<CustomerAndResListBean.Customers> needRecources = new ArrayList<>();
				for (int j = 0; j < mNeedResourceFactorCheckedList.size(); j++) {
					CustomerAndResListBean.Customers customers = new CustomerAndResListBean.Customers();
					customers.setCode(mNeedResourceFactorCheckedList.get(j).getCode());
					customers.setName(mNeedResourceFactorCheckedList.get(j).getName());
					customers.setDefaultUrl(mNeedResourceFactorCheckedList.get(j).getImageUrl().getDefaultUrl());
					customers.setSelectUrl(mNeedResourceFactorCheckedList.get(j).getImageUrl().getSelectedUrl());
					customers.setSelected(mNeedResourceFactorCheckedList.get(j).isRelevance());
					needRecources.add(customers);
				}
				supplyAndResFactorBean.setList(needRecources);
				list.add(supplyAndResFactorBean);
			}
			//组合好的新数据list 所有要用到的数据塞到fragment中
			CustomerAndResBean customerAndResBean = new CustomerAndResBean();
			customerAndResBean.setListBeen(list);
			Bundle bundle = new Bundle();
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA, customerAndResBean);
			if (mRecourseFactorShipBean != null) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_LAST_RESCOURCE_FACTOR_SHIP_CHECKED, mRecourseFactorShipBean);
			}
			ResourceFactorShipViewPagerFragment resourceShipViewPagerFragment = new ResourceFactorShipViewPagerFragment();
			resourceShipViewPagerFragment.setArguments(bundle);
			pushFragment(resourceShipViewPagerFragment);
		}

	}


	/**
	 * ('CUSTOMER', '选择客户');
	 ('CUSTOMER_AND_RESOURCE', '关联客户和资源');
	 ('HAVE_RESOURCE_FACTORS', '拥有的资源要素');
	 ('INDUSTRY', '选择行业');
	 ('NEED_RESOURCE_FACTORS', '需要的资源要素');
	 ('PROVIDER', '选择供应商');
	 ('PROVIDER_AND_RESOURCE_FACTORS', '关联供应商和资源要素');
	 ('RESOURCE', '提供的资源');

	 */

	/**
	 * 每个竖直listview的点击事件
	 *
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (isShow) {
			((BaseCardDeskActivity) mActivity).hideTitleBt();
		}
		isShow = !isShow;
		int clickId = parent.getId();
		//客户
		if (clickId == R.id.client_lv) {
			Bundle bundle = new Bundle();
			List<SourceListBean> mSourceListBeen = getSourceList("CUSTOMER");
			if (mCustomerCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_CHECKED_DATA, (Serializable) mCustomerCheckedList);
			}
			if (mBusId != null) {
				bundle.putString(ContractConstant.KEY_BUNDLE_CONTRACT_BUID, mBusId);
			}
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_SERVIC_CLIENTS_DATA, (Serializable) mSourceListBeen);
			ManySeekBarFragment manySeekBarFragment = new ManySeekBarFragment();
			manySeekBarFragment.setArguments(bundle);
			pushFragment(manySeekBarFragment);
			//资源
		} else if (clickId == R.id.resource_lv) {
			ModuleQueryRequest moduleQueryRequest = new ModuleQueryRequest();
			List<SourceListBean> sourceList = getSourceList("RESOURCE");
			moduleQueryRequest.setSourceList(sourceList);
			ModuleQueryRequest.BaseQuery baseQuery = new ModuleQueryRequest.BaseQuery();
			baseQuery.setPage(1);
			baseQuery.setSize(10000);
			moduleQueryRequest.setBaseQuery(baseQuery);
			moduleQueryRequest.setSourceModule("RESOURCE");
			moduleQueryRequest.setTaskType("task");
			if (mBusId != null) {
				moduleQueryRequest.setBuId(mBusId);
			}

			if (mResourceCheckedList.size() > 0) {
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_CHECKED_DATA, (Serializable) mResourceCheckedList);
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_QUARY_PARAMS, moduleQueryRequest);
				if (mBusId != null) {
					bundle.putString(ContractConstant.KEY_BUNDLE_CONTRACT_BUID, mBusId);
				}
				SupplyOfResourceViewPagerFragment supplyOfResourcesViewPagerFragment = new SupplyOfResourceViewPagerFragment();
				supplyOfResourcesViewPagerFragment.setArguments(bundle);
				pushFragment(supplyOfResourcesViewPagerFragment);
			} else {
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_QUARY_PARAMS, (Serializable) moduleQueryRequest);

				if (mBusId != null) {
					bundle.putString(ContractConstant.KEY_BUNDLE_CONTRACT_BUID, mBusId);
				}

				SupplyOfResourceViewPagerFragment supplyOfResourcesViewPagerFragment = new SupplyOfResourceViewPagerFragment();
				supplyOfResourcesViewPagerFragment.setArguments(bundle);
				pushFragment(supplyOfResourcesViewPagerFragment);
			}

			//资源要素
		} else if (clickId == R.id.resource_factor_lv) {
			ModuleQueryRequest mHaveModuleQueryRequest = getResourceFactorParams("HAVE_RESOURCE_FACTORS");
			ModuleQueryRequest mNeedModuleQueryRequest = getResourceFactorParams("NEED_RESOURCE_FACTORS");

			if (mBusId != null) {
				mHaveModuleQueryRequest.setBuId(mBusId);
				mNeedModuleQueryRequest.setBuId(mBusId);
			}
			Bundle bundle = new Bundle();
			if (mHaveResourceFactorCheckedList != null && mHaveResourceFactorCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_CHECKED_DATA, (Serializable) mHaveResourceFactorCheckedList);
			}
			if (mNeedResourceFactorCheckedList != null && mNeedResourceFactorCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA, (Serializable) mNeedResourceFactorCheckedList);
			}
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_PARAMS, mNeedModuleQueryRequest);
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_PARAMS, mHaveModuleQueryRequest);
			ResourceFactorMainFragment resourceFactorMainFragment = new ResourceFactorMainFragment();
			resourceFactorMainFragment.setArguments(bundle);
			pushFragment(resourceFactorMainFragment);


		} else if (clickId == R.id.supplier_lv) {
			Bundle bundle = new Bundle();
			List<SourceListBean> mSourceListBeen = getSourceList("PROVIDER");
			if (mSupplierCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_CHECKED_DATA, (Serializable) mSupplierCheckedList);
			}
			bundle.putSerializable(ContractConstant.KEY_BUNDLE_SUPPLY_PARAMS, (Serializable) mSourceListBeen);
			SupplySeekBarFragment manySeekBarFragment = new SupplySeekBarFragment();
			manySeekBarFragment.setArguments(bundle);
			pushFragment(manySeekBarFragment);

		}


	}

	@NonNull
	private ModuleQueryRequest getResourceFactorParams(String type) {
		ModuleQueryRequest moduleQueryRequest = new ModuleQueryRequest();
		List<SourceListBean> sourceList = getSourceList(type);
		moduleQueryRequest.setSourceList(sourceList);
		ModuleQueryRequest.BaseQuery baseQuery = new ModuleQueryRequest.BaseQuery();
		baseQuery.setPage(1);
		baseQuery.setSize(10000);
		moduleQueryRequest.setBaseQuery(baseQuery);
		moduleQueryRequest.setSourceModule(type);
		moduleQueryRequest.setTaskType("task");
		return moduleQueryRequest;
	}

	/**
	 * 点击行业返回的数据
	 *
	 * @param checkIndusEvent
	 */
	public void onEventMainThread(CheckIndusEvent checkIndusEvent) {
		if (checkIndusEvent.getIndustryChecks().size() == 1) {
			mIndustryTv.setText(checkIndusEvent.getIndustryChecks().get(0).getName());
		} else if (checkIndusEvent.getIndustryChecks().size() >= 2) {
			mIndustryTv.setText(checkIndusEvent.getIndustryChecks().size() + "个行业");
		}
		mIndustryCheckedList = checkIndusEvent.getIndustryChecks();
		checkCanclick();

	}

	/**
	 * 点击资源返回的数据
	 *
	 * @param checkIndusEvent
	 */
	private List<String> lastCheckIndList ;
	public void onEventMainThread(CheckResEvent checkIndusEvent) {

		boolean isContain = true;
		if (lastCheckIndList != null && checkIndusEvent != null && checkIndusEvent.getIndustryChecks().size() > 0) {
			if (lastCheckIndList.size() == checkIndusEvent.getIndustryChecks().size()) {
				for (int i = 0; i < checkIndusEvent.getIndustryChecks().size(); i++) {
					if (!lastCheckIndList.contains(checkIndusEvent.getIndustryChecks().get(i).getName())) {
						isContain = false;
					}
				}
				if (isContain) {
					return;
				}
			}
		}
		mResourceCheckedList = checkIndusEvent.getIndustryChecks();
		lastCheckIndList = new ArrayList<>();
		for (int i = 0; i < mResourceCheckedList.size(); i++) {
			lastCheckIndList.add(mResourceCheckedList.get(i).getName());
		}
		if (mResourceCheckedList.size() > 0) {
			mResourcePinCardBeanList = mResourceCheckedList;
			mResourceAdapter = new PinCardAdapter(mResourceCheckedList, mActivity, mLvHeight, R.layout.rescource_pincard_item);
			mResourceLv.setAdapter(mResourceAdapter);
		}
		if (mCustomerCheckedList.size() > 0 && mResourceCheckedList.size() > 0) {
			mResourceShipLayout.setEnabled(true);
			mResourceShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
			mResourceShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_relationship_white));
			mResourceShipLayout.setBackground(mActivity.getResources().getDrawable(R.drawable.rescource_background));
		} else {
			mResourceShipLayout.setEnabled(false);
			mResourceShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));
			mResourceShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_relationship));
			mResourceShipLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
		}
		checkCanclick();
		showTips(mResourceCheckedList, mCustomerAndResBean, "请为新增的提供服务添加关系", "取消的提供服务对应的关系已删除");
	}


	/**
	 * 判断关联关系结束
	 */


	/**
	 * 点击资源要素返回的数据
	 *
	 * @param checkIndusEvent
	 */
	private List<String> mLastCheckResList;
	public void onEventMainThread(CheckAllResFactorEvent checkIndusEvent) {
		boolean isContain = true;
		if (mLastCheckResList != null && checkIndusEvent != null && checkIndusEvent.getmNeedResFactorCheckedList().size() > 0) {
			if (mLastCheckResList.size() == checkIndusEvent.getmNeedResFactorCheckedList().size()) {
				for (int i = 0; i < checkIndusEvent.getmNeedResFactorCheckedList().size(); i++) {
					if (!mLastCheckResList.contains(checkIndusEvent.getmNeedResFactorCheckedList().get(i).getName())) {
						isContain = false;
					}
				}
				if (isContain) {
					return;
				}
			}
		}

		int haveResFactorShowCount = 0;
		mNeedResourceFactorCheckedList = checkIndusEvent.getmNeedResFactorCheckedList();
		mHaveResourceFactorCheckedList = checkIndusEvent.getmHaveResFactorCheckedList();
		mLastCheckResList = new ArrayList<>();
		for (int i = 0; i < mNeedResourceFactorCheckedList.size(); i++) {
			mLastCheckResList.add(mNeedResourceFactorCheckedList.get(i).getName());
		}
		//开始整理数据
		List<ChooseModuleBean.ModuleList.BeanList> mShowCheckedList = new ArrayList<>();
		//判断需要的资源要素数据有几个，如果大于两个只取2个
		if (mNeedResourceFactorCheckedList.size() >= 3) {
			haveResFactorShowCount = 3;
			mShowCheckedList.add(mNeedResourceFactorCheckedList.get(0));
			mShowCheckedList.add(mNeedResourceFactorCheckedList.get(1));
			mShowCheckedList.add(mNeedResourceFactorCheckedList.get(2));
		} else if (mNeedResourceFactorCheckedList.size() == 1) {
			//不应该存在0个所以直接添加第一个
			haveResFactorShowCount = 1;
			mShowCheckedList.add(mNeedResourceFactorCheckedList.get(0));
		} else if (mNeedResourceFactorCheckedList.size() == 2) {
			haveResFactorShowCount = 2;
			mShowCheckedList.add(mNeedResourceFactorCheckedList.get(0));
			mShowCheckedList.add(mNeedResourceFactorCheckedList.get(1));
		}
		//需要的资源要素
		if (mHaveResourceFactorCheckedList.size() > 0) {
			mShowCheckedList.addAll(mHaveResourceFactorCheckedList);
		}
		if (mShowCheckedList.size() > 0) {
			mResourceFactorPinCardBeanList = mShowCheckedList;
			mResourceAdapter = new PinCardAdapter(mResourceFactorPinCardBeanList, mActivity, mLvHeight, R.layout.rescource_factort_pincard_item, haveResFactorShowCount);
			mResourceFactorLv.setAdapter(mResourceAdapter);
		}

		if (mHaveResourceFactorCheckedList.size() > 0 && mNeedResourceFactorCheckedList.size() > 0 && mSupplierCheckedList.size() > 0) {
			mResourceFactorShipLayout.setEnabled(true);
			mResourceFactorShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
			mResourceFactorShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_factor_relation_white));
			mResourceFactorShipLayout.setBackground(mActivity.getResources().getDrawable(R.drawable.supplier_background));
		} else {
			mResourceFactorShipLayout.setEnabled(false);
			mResourceFactorShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));
			mResourceFactorShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_factor_relation));
			mResourceFactorShipLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
		}
		checkCanclick();
		showInnerTips(mNeedResourceFactorCheckedList, mRecourseFactorShipBean, "请为新增的资源添加关系", "取消的资源对应的关系已删除");

	}

	/**
	 * 点击客户返回的数据
	 *
	 * @param checkIndusEvent
	 */
	public void onEventMainThread(CheckCustomersEvent checkIndusEvent) {
		if (checkIndusEvent != null && checkIndusEvent.getmCustomerCheckedList() != null && checkIndusEvent.getmCustomerCheckedList().size() > 0) {
			boolean isContain = true;
			List<String> lastCheckNames = new ArrayList<>();
			List<String> nowCheckNames = new ArrayList<>();
			if (mCustomerCheckedList.size() == checkIndusEvent.getmCustomerCheckedList().size()) {
				for (int i = 0; i < mCustomerCheckedList.size(); i++) {
					lastCheckNames.add(mCustomerCheckedList.get(i).getName());
				}
				for (int i = 0; i < checkIndusEvent.getmCustomerCheckedList().size(); i++) {
					nowCheckNames.add(checkIndusEvent.getmCustomerCheckedList().get(i).getName());
				}
				if (lastCheckNames.size() > 0 && nowCheckNames.size() > 0) {
					for (int i = 0; i < nowCheckNames.size(); i++) {
						if (!lastCheckNames.contains(nowCheckNames.get(i))) {
							isContain = false;
						}
					}
				}
				if (isContain) {
					return;
				}
			}
		}
		mCustomerCheckedList = checkIndusEvent.getmCustomerCheckedList();
		if (mCustomerCheckedList.size() > 0) {
			List<Double> list = new ArrayList<>();
			for (int i = 0; i < mCustomerCheckedList.size(); i++) {
				list.add((double) mCustomerCheckedList.get(i).getProgress());
			}
			List<Double> newList = ArithMetic.getPinRatio(ArithMetic.getPinRatio(ArithMetic.getPinRatio(list, 15d), 15d), 15d);
			if (newList != null && newList.size() > 0 && mCustomerCheckedList.size() == newList.size()) {
				for (int i = 0; i < newList.size(); i++) {
					mCustomerCheckedList.get(i).setScale(newList.get(i));
				}
			}
		}
		if (mCustomerCheckedList.size() > 0) {
			mClientPinCardBeanList = mCustomerCheckedList;
			mClientAdapter = new PinCardAdapter(mCustomerCheckedList, mActivity, mLvHeight, R.layout.client_pincard_item);
			mClientLv.setAdapter(mClientAdapter);
		}
		if (mCustomerCheckedList.size() > 0 && mResourceCheckedList.size() > 0) {
			mResourceShipLayout.setEnabled(true);
			mResourceShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
			mResourceShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_relationship_white));
			mResourceShipLayout.setBackground(mActivity.getResources().getDrawable(R.drawable.rescource_background));
		} else {
			mResourceShipLayout.setEnabled(false);
			mResourceShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));
			mResourceShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_relationship));
			mResourceShipLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
		}
		checkCanclick();
		showInnerTips(mCustomerCheckedList, mCustomerAndResBean, "请为新增的客户类型添加关系", "取消的客户类型对应的关系已删除");
	}

	/**
	 * 点击供应商返回的数据
	 *
	 * @param checkIndusEvent
	 */
	public void onEventMainThread(CheckSupplyEvent checkIndusEvent) {
		if (checkIndusEvent != null && checkIndusEvent.getmCustomerCheckedList() != null && checkIndusEvent.getmCustomerCheckedList().size() > 0) {
			boolean isContain = true;
			List<String> lastCheckNames = new ArrayList<>();
			List<String> nowCheckNames = new ArrayList<>();
			if (mSupplierCheckedList.size() == checkIndusEvent.getmCustomerCheckedList().size()) {
				for (int i = 0; i < mSupplierCheckedList.size(); i++) {
					lastCheckNames.add(mSupplierCheckedList.get(i).getName());
				}
				for (int i = 0; i < checkIndusEvent.getmCustomerCheckedList().size(); i++) {
					nowCheckNames.add(checkIndusEvent.getmCustomerCheckedList().get(i).getName());
				}
				if (lastCheckNames.size() > 0 && nowCheckNames.size() > 0) {
					for (int i = 0; i < nowCheckNames.size(); i++) {
						if (!lastCheckNames.contains(nowCheckNames.get(i))) {
							isContain = false;
						}
					}
				}
				if (isContain) {
					return;
				}
			}
		}
		mSupplierCheckedList = checkIndusEvent.getmCustomerCheckedList();
		if (mSupplierCheckedList.size() > 0) {
			List<Double> list = new ArrayList<>();
			for (int i = 0; i < mSupplierCheckedList.size(); i++) {
				list.add((double) mSupplierCheckedList.get(i).getProgress());
			}
			List<Double> newList = ArithMetic.getPinRatio(ArithMetic.getPinRatio(ArithMetic.getPinRatio(list, 15d), 15d), 15d);
			if (newList != null && newList.size() > 0 && mSupplierCheckedList.size() == newList.size()) {
				for (int i = 0; i < newList.size(); i++) {
					mSupplierCheckedList.get(i).setScale(newList.get(i));
				}
			}
		}
		if (mSupplierCheckedList.size() > 0) {
			mSupplierPinCardBeanList = mSupplierCheckedList;
			mClientAdapter = new PinCardAdapter(mSupplierCheckedList, mActivity, mLvHeight, R.layout.supplier_pincard_item);
			mSupplierLv.setAdapter(mClientAdapter);
		}
		if (mHaveResourceFactorCheckedList.size() > 0 && mNeedResourceFactorCheckedList.size() > 0 && mSupplierCheckedList.size() > 0) {
			mResourceFactorShipLayout.setEnabled(true);
			mResourceFactorShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
			mResourceFactorShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_factor_relation_white));
			mResourceFactorShipLayout.setBackground(mActivity.getResources().getDrawable(R.drawable.supplier_background));
		} else {
			mResourceFactorShipLayout.setEnabled(false);
			mResourceFactorShipTv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));
			mResourceFactorShipIv.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.resource_factor_relation));
			mResourceFactorShipLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
		}
		checkCanclick();

		showTips(mSupplierCheckedList, mRecourseFactorShipBean, "请为新增的供应商类型添加关系", "取消的供应商类型对应的关系已删除");

	}

	/**
	 * 点击资源关系返回的数据
	 *
	 * @param checkResourceShipEvent
	 */
	public void onEventMainThread(CheckResourceShipEvent checkResourceShipEvent) {
		mCustomerAndResBean = checkResourceShipEvent.getCustomerAndResBean();
		if (mCustomerCheckedList.size() > 0 && mResourceCheckedList.size() > 0) {
			if (mCustomerAndResBean != null && mCustomerAndResBean.getListBeen().size() > 0) {
				if (!isShowPop) {
					showPop();
				}
			}
		}


	}

	/**
	 * 点击资源要素关系返回的数据
	 *
	 * @param checkResourceShipEvent
	 */
	public void onEventMainThread(CheckResourceFactorShipEvent checkResourceShipEvent) {
		mRecourseFactorShipBean = checkResourceShipEvent.getCustomerAndResBean();
		if (mNeedResourceFactorCheckedList.size() > 0
				&& mHaveResourceFactorCheckedList.size() > 0 && mSupplierCheckedList.size() > 0) {
			if (mRecourseFactorShipBean != null && mRecourseFactorShipBean.getListBeen().size() > 0) {
				if (!isShowPop) {
					showPop();
				}
			}
		}

	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	public String listToString(List<String> stringList) {
		if (stringList == null) {
			return null;
		}
		StringBuilder result = new StringBuilder();
		boolean flag = false;
		for (String string : stringList) {
			if (flag) {
				result.append(",");
			} else {
				flag = true;
			}
			result.append(string);
		}
		return result.toString();
	}

	/**
	 * private List<ChooseModuleBean.ModuleList.BeanList> mIndustryCheckedList = new ArrayList<>(); //行业已选的条目
	 * private List<ChooseModuleBean.ModuleList.BeanList> mCustomerCheckedList = new ArrayList<>(); //客户已选的条目
	 * private List<ChooseModuleBean.ModuleList.BeanList> mResourceCheckedList = new ArrayList<>(); //资源已选的条目
	 * private List<ChooseModuleBean.ModuleList.BeanList> mNeedResourceFactorCheckedList = new ArrayList<>(); //需要的资源要素已选的条目
	 * private List<ChooseModuleBean.ModuleList.BeanList> mHaveResourceFactorCheckedList = new ArrayList<>(); //拥有的资源要素已选的条目
	 * <p>
	 * private List<ChooseModuleBean.ModuleList.BeanList> mSupplierCheckedList = new ArrayList<>(); //供应商已选的条目
	 * int mLvHeight;
	 * CustomerAndResBean mCustomerAndResBean;
	 * <p>
	 * CustomerAndResBean mRecourseFactorShipBean;
	 *
	 * @return
	 */
	private void checkCanclick() {
		if (mIndustryCheckedList.size() > 0 && mCustomerCheckedList.size() > 0 && mResourceCheckedList.size() > 0 && mNeedResourceFactorCheckedList.size() > 0
				&& mHaveResourceFactorCheckedList.size() > 0 && mSupplierCheckedList.size() > 0) {
			if (mCustomerAndResBean != null && mCustomerAndResBean.getListBeen().size() > 0 || mRecourseFactorShipBean != null
					&& mRecourseFactorShipBean.getListBeen().size() > 0) {
				if (!isShowPop) {
					showPop();
				}
			}
		}
	}

	private boolean isShowPop;
	private void showPop() {
		View PopItemView = View.inflate(getActivity(), R.layout.popup_confirm_planning, null);
		EasyPopupWindow mEasyPopupWindow = new EasyPopupWindow.Builder(getActivity())
				.setView(PopItemView)
				.setSize(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
				.setOutsideTouchHide(true)
				.setBackgroundAlpha(0.6f)
				.create();
		int loc[] = new int[2];
		mIndusLayout.getLocationOnScreen(loc);
		mEasyPopupWindow.getContentView().measure(0, 0);
		int width = mEasyPopupWindow.getContentView().getMeasuredWidth();
		mEasyPopupWindow.setAnimationStyle(R.style.popwin_anim_by_businessunit);
		DisplayMetrics metric = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		int widthPixels = metric.widthPixels;     // 屏幕宽度
		mEasyPopupWindow.showAtLocation(mIndusLayout, Gravity.LEFT | Gravity.TOP, widthPixels / 2 - width / 2 + 20, loc[1]);
		isShowPop = true;
	}


	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (backListenerEvent.getMsg().equals("TaskPinCardMainFragment")) {
			if (mIndustryCheckedList.size() > 0 || mCustomerCheckedList.size() > 0 || mResourceCheckedList.size() > 0 || mNeedResourceFactorCheckedList.size() > 0
					|| mHaveResourceFactorCheckedList.size() > 0 || mSupplierCheckedList.size() > 0 || mCustomerAndResBean != null && mCustomerAndResBean.getListBeen().size() > 0 || mRecourseFactorShipBean != null
					&& mRecourseFactorShipBean.getListBeen().size() > 0) {

				DialogTools.getInstance().showTCDialog(mActivity, "确认返回", "内容未提交，返回将不会保存，确认返回？", "再看看", "返回", new DialogTools.ConfirmListener() {
					@Override
					public void setConfirmListener(Dialog dialog, View view, String s) {
						dialog.dismiss();

					}
				}, new DialogTools.DismissListener() {
					@Override
					public void setDismissListener(Dialog dialog, View view) {
						dialog.dismiss();
						popFragment();
					}
				});
			} else {
				popFragment();

			}
		}
	}


	private void showTips(List<ChooseModuleBean.ModuleList.BeanList> mResourceCheckedList, CustomerAndResBean mCustomerAndResBean, String addTip, String deleteTip) {
		/**
		 * ========================================================判断关联关系开始
		 */
		if (mCustomerAndResBean == null || mResourceCheckedList.size() == 0) {
			return;
		}
		//只有关联过的map
		Map<String, List<CustomerAndResListBean.Customers>> connectionMap = new HashMap<>();
		for (int i = 0; i < mCustomerAndResBean.getListBeen().size(); i++) {
			for (int j = 0; j < mCustomerAndResBean.getListBeen().get(i).getList().size(); j++) {
				if (mCustomerAndResBean.getListBeen().get(i).getList().get(j).isSelected()) {
					connectionMap.put(mCustomerAndResBean.getListBeen().get(i).getName(), mCustomerAndResBean.getListBeen().get(i).getList());
				}
			}
		}
		//只有关联过的List
		List<String> connectionList = new ArrayList<>();
		for (Map.Entry<String, List<CustomerAndResListBean.Customers>> entry : connectionMap.entrySet()) {
			connectionList.add(entry.getKey());
		}

		//所有的资源关系list
		List<String> allShipList = new ArrayList<>();
		for (int i = 0; i < mCustomerAndResBean.getListBeen().size(); i++) {
			allShipList.add(mCustomerAndResBean.getListBeen().get(i).getName());
		}

		//点击资源返回的选中的list
		List<String> resChedkedList = new ArrayList<>();
		for (int i = 0; i < mResourceCheckedList.size(); i++) {
			resChedkedList.add(mResourceCheckedList.get(i).getName());
		}

		//判断是什么都不做的情况 ，只是点击了下确定按钮
		boolean doNothing = false;
		if (resChedkedList.size() == allShipList.size()) {
			for (int i = 0; i < resChedkedList.size(); i++) {
				if (!allShipList.contains(resChedkedList.get(i))) {
					doNothing = true;
				}
			}
			if (!doNothing) {
				return;
			}
		}

		//判断是删除情况， 就是选中的list中，除了关联以外的项目，其他全部都在关系的所有列表中
		List<String> newList = new ArrayList<>();
		for (int i = 0; i < resChedkedList.size(); i++) {
			if (!connectionList.contains(resChedkedList.get(i))) {
				newList.add(resChedkedList.get(i));
			}
		}
		boolean isDelete = true;
		for (int i = 0; i < newList.size(); i++) {
			if (!allShipList.contains(newList.get(i))) {
				isDelete = false;
			}
		}
		if (isDelete) {
			//提示删除
			ToastUtils.showToast(deleteTip);
		} else {
			ToastUtils.showToast(addTip);
		}
	}


	private void showInnerTips(List<ChooseModuleBean.ModuleList.BeanList> mResourceCheckedList, CustomerAndResBean mCustomerAndResBean, String addTip, String deleteTip) {
		/**
		 * ========================================================判断关联关系开始
		 */
		if (mCustomerAndResBean == null || mResourceCheckedList.size() == 0) {
			return;
		}
		//只有关联过的map
		Map<String, List<CustomerAndResListBean.Customers>> connectionMap = new HashMap<>();
		for (int i = 0; i < mCustomerAndResBean.getListBeen().size(); i++) {
			for (int j = 0; j < mCustomerAndResBean.getListBeen().get(i).getList().size(); j++) {
				if (mCustomerAndResBean.getListBeen().get(i).getList().get(j).isSelected()) {
					connectionMap.put(mCustomerAndResBean.getListBeen().get(i).getList().get(j).getName(), mCustomerAndResBean.getListBeen().get(i).getList());
				}
			}
		}
		//只有关联过的List
		List<String> connectionList = new ArrayList<>();
		for (Map.Entry<String, List<CustomerAndResListBean.Customers>> entry : connectionMap.entrySet()) {
			connectionList.add(entry.getKey());
		}

		//所有的资源关系list
		List<String> allShipList = new ArrayList<>();
		for (int i = 0; i < mCustomerAndResBean.getListBeen().get(0).getList().size(); i++) {
			allShipList.add(mCustomerAndResBean.getListBeen().get(0).getList().get(i).getName());
		}

		//点击资源返回的选中的list
		List<String> resChedkedList = new ArrayList<>();
		for (int i = 0; i < mResourceCheckedList.size(); i++) {
			resChedkedList.add(mResourceCheckedList.get(i).getName());
		}

		//判断是什么都不做的情况 ，只是点击了下确定按钮
		boolean doNothing = false;
		if (resChedkedList.size() == allShipList.size()) {
			for (int i = 0; i < resChedkedList.size(); i++) {
				if (!allShipList.contains(resChedkedList.get(i))) {
					doNothing = true;
				}
			}
			if (!doNothing) {
				return;
			}
		}

		//判断是删除情况， 就是选中的list中，除了关联以外的项目，其他全部都在关系的所有列表中
		List<String> newList = new ArrayList<>();
		for (int i = 0; i < resChedkedList.size(); i++) {
			if (!connectionList.contains(resChedkedList.get(i))) {
				newList.add(resChedkedList.get(i));
			}
		}
		boolean isDelete = true;
		for (int i = 0; i < newList.size(); i++) {
			if (!allShipList.contains(newList.get(i))) {
				isDelete = false;
			}
		}
		if (isDelete) {
			//提示删除
			ToastUtils.showToast(deleteTip);
		} else {
			ToastUtils.showToast(addTip);
		}
	}


}
